package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyBiChe;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeBiChe;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.ArrayList;
import java.util.List;

/**
 * 安心 - 碧澈
 *  链接：https://www.kdocs.cn/l/cdZr52Hwmrci
 *
 * @author gwc
 * @create 2025-08-21 下午2:08
 */
@ApiPlan(productId = "378", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskBiChe implements ApiOrgPhoneMaskConvertHandler {

    private static final String APP_ID = "600188";
    private static final String APP_SECRET = "JAAV88JKKR1NDwN702C3Z0H3Ka29j800";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        // 表单类型
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        // 创建撞库实体类
        OrgDataExchangeBiChe biCheData = new OrgDataExchangeBiChe();
        biCheData.setMobile(apiDataExchangeParameter.getPhoneMask());
        biCheData.setAppSecret(APP_SECRET);
        biCheData.setAppId(APP_ID);
        return biCheData;
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        JSONArray data = jsonObject.getJSONArray("data");
        if (ValidParam.isEmpty(data)) {
            return RespOrgPhoneMaskDataExchange.ofSuc();
        }
        RespOrgPhoneMaskDataExchange respOrgPhoneMaskDataExchange = RespOrgPhoneMaskDataExchange.ofSuc();
        List<String> md5 = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            // 获取数组中的每个元素
            String phoneMd5 = data.getString(i);
            md5.add(phoneMd5);
        }
        respOrgPhoneMaskDataExchange.setListPhoneMd5(md5);
        return respOrgPhoneMaskDataExchange;
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 表单类型
        apiApplyParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 创建对应的进件实体类
        OrgApplyBiChe biCheData = new OrgApplyBiChe();
        // 应用 ID
        biCheData.setAppId(APP_ID);
        // 应用密钥
        biCheData.setAppSecret(APP_SECRET);
        // 渠道（如：baidu）
        biCheData.setPromotion("安心分期");
        // 姓名
        biCheData.setName(apiApplyParameter.getUserName());
        // 手机号码
        biCheData.setMobile(apiApplyParameter.getPhone());
        // 年龄（若没有传 0）
        biCheData.setAge(apiFieldConverter.getAgeInt());
        // 性别（1:男，2:女，无传 0）
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end(1);
        biCheData.setSex(sex);
        // 城市（如：天津市）
        biCheData.setCity(apiApplyParameter.getCity());
        // 是否有房（是传 1，否传 0）
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(0);
        biCheData.setIsHouse(house);
        // 是否有车（是传 1，否传 0）
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(0);
        biCheData.setIsCar(vehicle);
        // 是否有公司营业执照（是传 1，否传 0）
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 0)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "3", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "4", 1)
                .end(0);
        biCheData.setIsCompany(bizLicenseYear);
        // 是否有信用卡（是传 1，否传 0）
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 1)
                .when(apiApplyParameter.getCreditLimit(), "4", 1)
                .when(apiApplyParameter.getCreditLimit(), "5", 2)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end(0);
        biCheData.setIsCredit(creditCard);
        // 是否有保单（是传 1，否传 0）
        Integer commericalInsurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end(0);
        biCheData.setIsInsurance(commericalInsurance);
        // 是否有社保（是传 1，否传 0）
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end(0);
        biCheData.setIsSocial(socialSecurity);
        // 是否有公积金（是传 1，否传 0）
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 1)
                .when(apiApplyParameter.getProvidentFund(), "3", 1)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end(0);
        biCheData.setIsFund(providentFund);
        // 是否有打卡工资（是传 1，否传 0）
        Integer salaryMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 1)
                .end(0);
        biCheData.setIsWork(salaryMethod);
        // 是否有营业税（是传 1，否传 0）
        biCheData.setIsTax(0);
        // 是否有微粒贷（是传 1，否传 0）
        biCheData.setWebank(0);
        // 申请额度（单位:元）
        biCheData.setMoneyDemand(apiFieldConverter.getLoanAmountInt());
        // 将除了文档中的资质别的资质存储在content
        biCheData.setContent(apiFieldConverter.getExtendInfo());
        return biCheData;
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
